﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class getSamplePoints : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.Charset = "utf-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        string imageId = Request.QueryString[0].ToString().Trim();
        string result = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "\n";
        string SP_FILE_NAME = "";
        string DistrictId = "";

        string[] ImageIdArray = imageId.Split('_');
        string samplingDateString = ImageIdArray[2] + "-" + ImageIdArray[3] + "-" + ImageIdArray[4];
        //samplingDateString = samplingDateString.Remove(4) + "-" + samplingDateString.Remove(0, 4).Remove(2) + "-" + samplingDateString.Remove(0, 6);


        //DateTime SamplingDate = Convert.ToDateTime(samplingDateString);


        string queryString = "select SP_FILE_NAME,DISTID from RS.DISTRICT_IMAGE_SP_REF where IMAGEID = '" + imageId + "'";
        DataSet ds = OracleDataBase.Query(queryString);
        DataTable dt = ds.Tables[0];
        if (dt.Rows.Count == 1)
        {
            SP_FILE_NAME = dt.Rows[0][0].ToString();
            DistrictId = dt.Rows[0][1].ToString();
        }
        //string IMAGE_NAME = dt.Rows[0][2].ToString();
        dt.Dispose();
        ds.Dispose();

        if (Convert.ToInt32(DistrictId) > 50)//判断该行政区是否是省级
        {
            queryString = "select POINTNAME,JIN,WEI from rs." + SP_FILE_NAME + " where distid = '" + DistrictId + "'";
        }
        else
        {
            queryString = "select POINTNAME,JIN,WEI from rs." + SP_FILE_NAME + " where rootid = '" + DistrictId + "'";
        }
        ds = OracleDataBase.Query(queryString);
        dt = ds.Tables[0];

        queryString = "select POINTNAME,LAI,MCL,SLFW,PLANTGDW FROM RS.SP_AGRI_PARAM WHERE SAMPDATE = to_date('" + samplingDateString + "','yyyy-mm-dd')";//'2005-1-1','yyyy-mm-dd'.ToShortDateString() + "'";
        DataSet ds1 = OracleDataBase.Query(queryString);
        DataTable dt1 = ds1.Tables[0];



        //将样点文件输出到XML文档
        result += "<SamplePoints>" + "\n";

        if (dt.Rows.Count > 0 && dt .Rows .Count ==dt1.Rows .Count )
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int k = 0; k < dt.Rows.Count; k++)
                {
                    if (dt1.Rows[i][0].ToString() == dt.Rows[k][0].ToString())
                    {
                        result += "<Point name = '" + dt.Rows[k][0].ToString() + "' Lon = '" + dt.Rows[k][1].ToString() + "' Lat = '" + dt.Rows[k][2].ToString() + "'";

                    }
                }
                for (int j = 0; j < dt1.Rows.Count; j++)
                {
                    if (dt1.Rows[j][0].ToString().ToUpper() == dt.Rows[i][0].ToString().ToUpper())
                    {
                        result += " LAI = '" + dt1.Rows[i][1].ToString() + "' MCL = '" + dt1.Rows[i][2].ToString() + "' SLFW = '" + dt1.Rows[i][3].ToString() + "' PLANTGDW = '" + dt1.Rows[i][4].ToString() + "'/>" + "\n";
                        break;
                    }
                }
            }
        }
        result += "</SamplePoints>";
        Response.Write(result);

        //RemoteSensingIndex.RSI.ReSamplePointRemoteSensingIndex pRsi = new RemoteSensingIndex.RSI.ReSamplePointRemoteSensingIndex();

        //if (pRsi.init())
        //{

        //    string baseLocation = System.Configuration.ConfigurationManager.AppSettings["DEFAULT_LOCATION"];// Server..MapPath("../"); //Environment.CurrentDirectory;
        //    string sdeServer = System.Configuration.ConfigurationManager.AppSettings["SDE_SERVER"];
        //    string sdeInstance = System.Configuration.ConfigurationManager.AppSettings["SDE_INSTANCE"];
        //    string sdeUser = System.Configuration.ConfigurationManager.AppSettings["SDE_USER"];
        //    string sdePWD = System.Configuration.ConfigurationManager.AppSettings["SDE_PWD"];

        //    //string sampleFileLocation = SP_FILE_NAME;

        //    result = pRsi.GetIndexDataSet(SP_FILE_NAME, imageId, baseLocation + "\\data\\", 3, sdeServer, sdeInstance, sdeUser, sdePWD);



        //    //string sampleFileLocation = baseLocation + "data\\" + fileNames;
        //    //result = pRsi.GetIndexDataSet(sampleFileLocation, baseLocation + "\\data\\");

        //    Response.Write(result);
        //}
    }
}
